package util.core.plugins.mycraft.cc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import entity.core.plugins.mycraft.cc.file;
import implate.core.plugins.mycraft.cc.Mycraft;

public class LoadBean extends Mycraft {
	private file fs = null;
	private Object variabled;
	private String packet = "";

	/**
	 * 从配置文件中加载对应属性并将其动态转换为类
	 * 
	 * @param fs
	 *            file 类
	 * @param obj
	 *            Object 需要将配置文件导出到的类
	 * @throws FileNotFoundException
	 *             不存在
	 * @return Object 通过 getVariabled() 获取返回数据
	 */
	public LoadBean(file fs, Object obj) throws FileNotFoundException {
		this.fs = fs;
		this.variabled = obj;
		if (!this.fs.getSuffix().isEmpty() || !this.fs.getSuffix().equals("null")) {
			if (this.fs.getSuffix().equals("yml")) { // yml 配置文件
				this.variabled = obj;
				this.LoadYamlFile(this.fs.getPath().toString());
			}
		}
	}

	private void SetValue(String key, Object value, Object obj) {
		Class<? extends Object> clazz = obj.getClass();
		Method[] methods = clazz.getDeclaredMethods();
		for (Method met : methods) {
			String tkey = key.substring(0, 1).toUpperCase() + key.substring(1, key.length());
			tkey = "set" + tkey;
			if (met.getName().equals(tkey)) {
				// Log.info(clazz.getName(),".",tkey, " => ", value.toString());
				try {
					met.invoke(obj, value);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	private void SplitData(List<String> reader, int line, int count, Object obj) {
		try {
			Pattern p = Pattern.compile("\\S"); // 获取第一个可见字符出现的位置
			Pattern t = Pattern.compile("\\:"); // 获取标记符位置, 方便切取键值
			Pattern n = Pattern.compile("\\#"); // 注释符标记
			for (int i = line; i < reader.size(); i++) {
				String tmpLine = reader.get(i); // 取行
				Matcher mm = n.matcher(tmpLine);
				if (mm.find()) { // 跳过注释行
					if ((int) mm.start() == 0)
						continue;
					else
						tmpLine = tmpLine.substring(0, mm.start()); // 去除注释内容
				}
				Matcher m0 = p.matcher(tmpLine); // 具备可识别字符
				if (m0.find()) { // got it
					int lineTab = m0.start(); // 取首字符位置
					tmpLine = tmpLine.trim(); // 去除两端空格
					int lineCout = tmpLine.length(); // 标准行长度
					Matcher m1 = t.matcher(tmpLine); // 具备标记符
					if (m1.find()) {
						int flag = m1.start(); // 取标识符位置
						String key = tmpLine.substring(0, flag).trim();
						if (flag == (lineCout - 1)) { // 判断标记符位置 是否具备子属性, 递归创建类
							String className = tmpLine.trim().substring(0, (tmpLine.length() - 1));
							Object jec = Class.forName(this.packet + "." + className).newInstance();
							SplitData(reader, (i + 1), (count + 2), jec);
							this.SetValue(className, jec, obj);
						} else {
							String value = tmpLine.substring((flag + 1), (tmpLine.length())).trim();
							// Log.info(" => ",key, " : ", value);
							if ((count + 2) == lineTab) { // 当前父属性的子属性
								// Log.info("son ", tmpLine);
								if (value.substring(0, 1).equals("\"")) { // 字符型值 String
									value = value.substring(1, (value.length() - 1)).trim();
									this.SetValue(key, value, obj);
								} else { // 整型值 int
									this.SetValue(key, Integer.parseInt(value), obj);
								}
							} else if ((count - 2) == lineTab) { // 当前父属性结束, return 递归
								// Log.info("jump ", tmpLine);
								continue;
							} else if (count == lineTab) { // 当前属性子属性
								// Log.info("bro ", tmpLine);
							} else { // 格式错误
								// Log.info("[Error] Format Fail, line[",i+1,"]");
							}
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Object LoadYamlFile(String string) throws FileNotFoundException {
		// TODO Auto-generated method stub
		File fs = new File(string);
		BufferedReader reader = null;
		if (fs.canRead()) {
			reader = new BufferedReader(new FileReader(fs)); // 加载文件
			List<String> strs = new ArrayList<String>();
			String str = "";
			try {
				while ((str = reader.readLine()) != null) {
					strs.add(str);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Pattern n = Pattern.compile("\\:"); // 标识符
			String stt = strs.get(0);
			Matcher n1 = n.matcher(stt);
			if (n1.find()) {
				Pattern m = Pattern.compile("\\#"); // 注释符
				Matcher m1 = m.matcher(stt);
				if (m1.find())
					stt = stt.substring(0, m1.start()).trim();
				this.packet = stt.substring((n1.start() + 1), stt.length()).trim();
				SplitData(strs, 1, 0, this.variabled);
			}
		}
		return null;
	}

	public Object getVariabled() {
		return variabled;
	}

	public void setVariabled(Object variabled) {
		this.variabled = variabled;
	}

	public String getPacket() {
		return packet;
	}

	public void setPacket(String packet) {
		this.packet = packet;
	}

}
